/**
 进货商品
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            upShopHouseInManageInfo: {
                rsId: '',
                shId: '',
                supplierName: '',
                resourceSuppliers: [],
                inoutOrderProducts: [],
                remark: '',
                totalMount: 0.00,
                orderType: '10000',
                shopId: vc.getCurrentCommunity().shopId
            }
        },
        _initMethod: function () {
            $that._listResourceSuppliers();
            //$that._listInoutOrder(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function () {
            vc.on('upShopHouseInManage', 'chooseProductAndSpec', function (_product) {
                $that.upShopHouseInManageInfo.inoutOrderProducts.push(_product);
            });
        },
        methods: {
            _doInoutOrder: function () {
                let _inoutOrderProducts = $that.upShopHouseInManageInfo.inoutOrderProducts;
                if (_inoutOrderProducts.length < 1) {
                    vc.toast('请选择商品');
                    return;
                }
                // _inoutOrderProducts.forEach(item => {
                //     item.valueId = item.defaultSpecValue.valueId;
                //     item.specName = item.defaultSpecValue.specName;
                // });
                vc.http.apiPost(
                    '/inoutOrder/saveInoutOrder',
                    JSON.stringify($that.upShopHouseInManageInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast(_json.msg);
                            vc.goBack();
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _openAddShopHouseModal: function () {
                vc.emit('chooseProductAndSpec', 'openChooseProductModel', {});
            },
            _goBack: function () {
                vc.goBack();
            },
            _deleteProduct: function (_inoutOrderProduct) {
                let _inoutOrderProducts = $that.upShopHouseInManageInfo.inoutOrderProducts;
                let index = _inoutOrderProducts.indexOf(_inoutOrderProduct);
                if (index > -1) {
                    _inoutOrderProducts.splice(index, 1);
                }
            },
            _listResourceSuppliers: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                        storeId:vc.getCurrentCommunity().storeId
                    }
                };
                //发送get请求
                vc.http.apiGet('resourceSupplier.listResourceSuppliers',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.upShopHouseInManageInfo.resourceSuppliers = _json.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _changeResourceSupp: function (_rsId) {
                $that.upShopHouseInManageInfo.resourceSuppliers.forEach(item => {
                    if (item.rsId == _rsId) {
                        $that.upShopHouseInManageInfo.resourceSuppliers = item.supplierName;
                    }
                });
            },
            _addSupplier: function () {
                vc.jumpToPage('/#/pages/goods/resourceSupplierManage?returnFlag=1');
            },
            _caculTotalMount: function () {
                let j = 0;
                $that.upShopHouseInManageInfo.inoutOrderProducts.forEach(item => {
                    if (!isNaN(item.count) && !isNaN(item.costPrice)) {
                        j += parseFloat(item.count) * parseFloat(item.costPrice).toFixed(2);
                    } else {
                        $that.upShopHouseInManageInfo.totalMount = "请输入正确的数字";
                        throw Error();
                    }
                });
                $that.upShopHouseInManageInfo.totalMount = j.toFixed(2);
            }
        }
    });
})(window.vc);
